package com.kang.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.alibaba.fastjson.JSON;
import com.kang.pojo.AjaxResult;
import com.kang.pojo.Role;
import com.kang.service.RoleService;
import com.vonechina.entity.page.RolePage;

/**
 * 角色管理
 * 
 * @author ysq
 *
 */
@Controller
@RequestMapping("role")
@SessionAttributes({ "rolePage" })
public class RoleController extends com.kang.controller.Controller {
	/**
	 * 角色服务
	 */
	@Resource
	RoleService rls;

	/**
	 * 根据条件搜索所有角色
	 * 
	 * @param session
	 * @param model
	 * @param rolePage
	 * @return
	 */
	@RequestMapping("selectByWhere")
	public String PurchaseorderSelectByWhere(HttpSession session, Model model,
			RolePage rolePage) {
		initRight(session);
		if (checkRight("查看角色") != SUCCESS) {
			return "basic/noRight";
		}
		model.addAttribute("isAdd", checkRight("新增角色") == SUCCESS);
		model.addAttribute("isEdit", checkRight("编辑角色") == SUCCESS);
		model.addAttribute("isDelete", checkRight("删除角色") == SUCCESS);

		List<Role> role = rls.selectRole(rolePage);// 根据条件查询角色role数据list
		model.addAttribute("role", role);

		rolePage.setPageUrl("role/selectByWhere.do");
		model.addAttribute("page", rolePage);
		return "system/role/role";
	}

	/**
	 * 根据角色roleID查询出角色role信息-用于修改编辑
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("getRoleById")
	@ResponseBody
	public String getRoleById(String id) {
		Role role = rls.getRoleById(id);// 根据角色roleID查询出角色role信息-用于修改编辑
		return JSON.toJSONString(role);
	}

	/**
	 * 根据roleID删除角色role
	 * 
	 * @param session
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping("delectrole")
	@ResponseBody
	public String delectRott(HttpSession session, Model model, Integer id) {
		initRight(session);
		AjaxResult result = new AjaxResult();
		if (checkRight("删除角色") != SUCCESS) {
			result.setResult("noRight");
			return JSON.toJSONString(result);
		}
		int usercount = rls.deleteone(id);// 根据roleID删除角色role

		if (usercount > 0) {
			result.setResult("success");
		} else {
			result.setResult("fail");
		}

		return JSON.toJSONString(result);
	}

	/**
	 * 根据roleID修改角色role
	 * 
	 * @param session
	 * @param model
	 * @param ro
	 * @return
	 */
	@RequestMapping("updaterole")
	@ResponseBody
	public String updateRott(HttpSession session, Model model, Role ro) {
		initRight(session);
		AjaxResult result = new AjaxResult();
		if (checkRight("编辑角色") != SUCCESS) {
			result.setResult("noRight");
			return JSON.toJSONString(result);
		}
		int userSum = rls.updateone(ro);// 根据roleID修改角色role
		if (userSum > 0) {
			result.setResult("success");
		} else {
			result.setResult("fail");

		}
		return JSON.toJSONString(result);
	}

	/**
	 * 根据角色名查询是否有相同的角色roleName，返回数量
	 * 
	 * @param rolename
	 * @return
	 */
	@RequestMapping("checkRole")
	@ResponseBody
	public String checkRole(String rolename) {
		AjaxResult result = new AjaxResult();
		int userSum = rls.selRoleByRolenamesSum(rolename);// 根据角色名查询是否有相同的角色roleName，返回数量
		if (userSum > 0) {
			result.setResult("fail");
		} else {
			result.setResult("success");
		}
		return JSON.toJSONString(result);
	}

	/**
	 * 添加角色role数据
	 * 
	 * @param session
	 * @param model
	 * @param rolename
	 * @param roleStatus
	 * @return
	 */
	@RequestMapping("addrole")
	@ResponseBody
	public String addRott(HttpSession session, Model model, String rolename,
			String roleStatus) {
		initRight(session);
		AjaxResult result = new AjaxResult();
		if (checkRight("新增角色") != SUCCESS) {
			result.setResult("noRight");
			return JSON.toJSONString(result);
		}
		Role ro = new Role();
		ro.setCreatedate(new Date());
		ro.setRolename(rolename);
		ro.setStatus(roleStatus);
		int rolecount = rls.insertone(ro);// 添加角色role数据
		if (rolecount >= 0) {
			result.setResult("success");
		}
		return JSON.toJSONString(result);
	}
}
